YAHOO.namespace('springmole');


/**
 * 
 */
YAHOO.springmole.SpringMole = function () {
    var VERSION = 'SpringMole @@version@@', 

    	Event = YAHOO.util.Event, 
		Dom = YAHOO.util.Dom, 
		Cookie = YAHOO.util.Cookie,
		
		aSortedDependency = [],
    
		CONNECTION_PANEL_ID = 'connection_panel_id', 
		STATUS_PANEL_ID = 'status_panel_id', 
		HIBERNATE_PANEL_ID = 'hibernate_panel_id', 
		QUARTZ_PANEL_ID = 'quartz_panel_id', 
		
		menuItemData,
		panelManager;
    
    
    panelManager = new YAHOO.widget.OverlayManager();
    
    
	/**
	 *
	 * @param {Object} panel
	 */
    function addPanel(panel) {
		panelManager.register(panel);
		panelManager.focus(panel);
	}

    /**
     * 
     * @param {Object} panelId
     */
    function getPanel(panelId) {
    	return panelManager.find(panelId);
    }
    
	/**
	 *
	 * @param {Object} panelId
	 */
	function selectPanel(panelId) {
		var panel = panelManager.find(panelId);
		if (panel) {
			panel.show();
			panelManager.focus(panel);
			return true;
		}
		return false;
	}
    
    function onMenuNewConnectionClick() {
		panelManager.find(CONNECTION_PANEL_ID).createNewConnectionNode();
	}
    
    function onMenuHibernateClick(index) {
		if (selectPanel(HIBERNATE_PANEL_ID)) {
			panelManager.find(HIBERNATE_PANEL_ID).cfg.setProperty('activetabindex', index);
		} else {
			var hibernatePanel = new YAHOO.springmole.HibernatePanel(HIBERNATE_PANEL_ID, {
				close : true,
				activetabindex : index,
				underlay : 'shadow',
				preventcontextoverlap : true,
				x : 400,
				y : 35
			});

			hibernatePanel.render(document.body);
			addPanel(hibernatePanel);
		}
	}

	function onMenuQuartzClick(index) {
		if (selectPanel(QUARTZ_PANEL_ID)) {
			panelManager.find(QUARTZ_PANEL_ID).cfg.setProperty('activetabindex', index);
		} else {
			var springMolePanel = new YAHOO.springmole.SpringMolePanel(QUARTZ_PANEL_ID, {
				close : true,
				underlay : 'none',
				preventcontextoverlap : true,
				x : 400,
				y : 100,
				width : '300px',
				height : '200px'
			});

			springMolePanel.render(document.body);
			addPanel(springMolePanel);
		}
	}

	function onMenuViewClick(p_oEvent, p_oMouseEvent, p_sPanelId) {
		var panel = panelManager.find(p_sPanelId);
		if (this.cfg.getProperty('checked')) {
			panel.hide();
			this.cfg.setProperty('checked', false);
		} else {
			panel.show();
			this.cfg.setProperty('checked', true);
		}
	}

	function onMenuAboutClick() {
		alert(VERSION);
	}

	menuItemData = [{
		text : "Connection",
		submenu : {
			id : 'connection_menu_id',
			zIndex : 1000,
			itemdata : [ {
				text : "New Connection",
				onclick : {
					fn : onMenuNewConnectionClick
				}
			} ]
		}
	}, {
		text : "Hibernate",
		submenu : {
			id : 'hibernate_menu_id',
			zIndex : 1000,
			itemdata : [{
				text : "Properties",
				onclick : {
					fn : function () {
						onMenuHibernateClick(0);
					}
				}
			}, {
				text : "Statistics",
				onclick : {
					fn : function () {
						onMenuHibernateClick(1);
					}
				}
			}]
		}
	}, {
		text : "Quartz",
		submenu : {
			id : 'quartz_menu_id',
			zIndex : 1000,
			itemdata : [ {
				text : "Properties",
				onclick : {
					fn : function () {
						onMenuQuartzClick(0);
					}
				}
			}, {
				text : "Jobs",
				onclick : {
					fn : function () {
						onMenuQuartzClick(1);
					}
				}
			}]
		}
	}, {
		text : "View",
		submenu : {
			id : 'view_menu_id',
			zIndex : 1000,
			itemdata : [{
				text : "Connections",
				checked : true,
				onclick : {
					fn : onMenuViewClick,
					obj : CONNECTION_PANEL_ID
				}
			}, {
				text : "Status",
				checked : true,
				onclick : {
					fn : onMenuViewClick,
					obj : STATUS_PANEL_ID
				}
			}]
		}
	}, {
		text : "Help",
		submenu : {
			id : 'help_menu_id',
			zIndex : 1000,
			itemdata : [{
				text : "About",
				onclick : {
					fn : onMenuAboutClick
				}
			}]
		}
	}];

	/**
	 *
	 */
	function init(p_aSortedDependency) {
		var oMenuBar,
			connectionPanel,
			statusPanel;
		
		aSortedDependency = p_aSortedDependency;
		
		oMenuBar = new YAHOO.widget.MenuBar('menubar_id', {
			zIndex : 1000,
			lazyload : true,
			itemdata : menuItemData,
			effect : {
				effect : YAHOO.widget.ContainerEffect.FADE,
				duration : 0.15
			}
		});
		oMenuBar.render(document.body);

		connectionPanel = new YAHOO.springmole.ConnectionPanel(CONNECTION_PANEL_ID, {
			close : false,
			constraintoviewport : true,
			x : 10,
			y : 35,
			width : '300px',
			height : '600px'
		});
		connectionPanel.render(document.body);
		addPanel(connectionPanel);

		statusPanel = new YAHOO.springmole.StatusPanel(STATUS_PANEL_ID, {
			close : false,
			constraintoviewport : true,
			x : Dom.getViewportWidth() - 315,
			y : Dom.getViewportHeight() - 75,
			width : '300px',
			height : '60px'
		});
		statusPanel.render(document.body);
		addPanel(statusPanel);
		
		//statusPanel.appendStatus("Initialization completed.");
		//statusPanel.appendStatus(VERSION);
	}

	
	return {
		version: VERSION,
		init: init,
		addPanel: addPanel,
		selectPanel: selectPanel,
		getPanel: getPanel
	};

}();
